package com.yyfax.pay.depository.controller.api;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.common.OpResponse;
import com.yyfax.pay.common.annotation.CheckSign;
import com.yyfax.pay.common.enums.FuiouDepositStateEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.model.to.YYPayApiReq2;
import com.yyfax.pay.depository.domain.FuiouWithdraw;
import com.yyfax.pay.depository.domain.FuiouWithdrawModifyCash;
import com.yyfax.pay.depository.service.inner.ChargeInnerService;
import com.yyfax.pay.depository.service.inner.FuiouWithdrawService;
import com.yyfax.pay.depository.vo.FuiouChargeTransferVO;
import com.yyfax.pay.depository.vo.FuiouChargeVO;
import com.yyfax.pay.depository.vo.FuiouWithdrawModifyCashVO;
import com.yyfax.pay.service.depository.ChargeService;
import com.yyfax.pay.vo.depository.EntrustChargeReq;
import com.yyfax.pay.vo.depository.ModifyWithdrawCashWayReq;
import com.yyfax.pay.vo.depository.QuickChargePayReq;
import com.yyfax.pay.vo.depository.QuickChargeSendSmsReq;
import com.yyfax.pay.vo.depository.RspContent;
import com.yyfax.pay.vo.depository.TransferChargeReq;
import com.yyfax.pay.vo.depository.TransferChargeRsp;
import com.yyfax.pay.vo.depository.WithdrawReq;

/**
 * 充值提现api入口
 * 
 * @author buyi
 * @date 2017-09-02 10:10:17
 * @since v2.0.0
 */
@RestController
public class ChargeWithdrawApiController {
	@Resource
	private ChargeService chargeService;

	@Resource
	private FuiouWithdrawService fuiouWithdrawService;

	@Resource
	private ChargeInnerService chargeInnerService;

	/**
	 * 快捷充值-发送短信验证码
	 *
	 * @author yejc
	 * @date 2017-09-20
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/fuiou/charge/shortcut/send_sms")
	@CheckSign
	public OpResponse quickChargeSendSms(@RequestBody @Valid YYPayApiReq2<QuickChargeSendSmsReq> req) throws Exception {
		chargeService.quickChargeSendSms(req.getMerchantCode(), req.getParams());
		return OpResponse.success();
	}

	/**
	 * 快捷充值-快捷支付
	 *
	 * @author yejc
	 * @date 2017-09-20
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/fuiou/charge/shortcut/pay")
	@CheckSign
	public OpResponse quickChargePay(@RequestBody @Valid YYPayApiReq2<QuickChargePayReq> req) throws Exception {
		return chargeService.quickCharge(req.getParams(), req.getMerchantCode());
	}

	/**
	 * 委托提现
	 *
	 * @author yejc
	 * @date 2017-09-20
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/fuiou/withdraw/entrust")
	@CheckSign
	public OpResponse entrustWithdraw(@RequestBody @Valid YYPayApiReq2<WithdrawReq> req) throws Exception {
		FuiouWithdraw withdraw = fuiouWithdrawService.entrustWithdraw(req.getParams(), req.getMerchantCode());

		OpResponse response = new OpResponse();
		RspContent rspContent = new RspContent();
		if (withdraw.getState().equals(FuiouDepositStateEnum.DEALING.getValue())) {
			response.setCode(PayExceptionCode.PROCESSING.getCode());
			response.setDisplay(PayExceptionCode.PROCESSING.getDesc());
			return response;
		} else if (withdraw.getState().equals(FuiouDepositStateEnum.ACCEPTED.getValue())) {
			rspContent.setBusiId(withdraw.getBusiId());
			rspContent.setState(RspContent.PROCESSING);
		} else if (withdraw.getState().equals(FuiouDepositStateEnum.SUCCESS.getValue())) {
			rspContent.setBusiId(withdraw.getBusiId());
			rspContent.setState(RspContent.SUCCESS);
		}
		response.setCode(withdraw.getRspCode());
		response.setDisplay(withdraw.getRspMsg());
		response.setContent(rspContent);

		return response;
	}

	/**
	 * 委托充值
	 *
	 * @author yejc
	 * @date 2017-09-21
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/fuiou/charge/entrust")
	@CheckSign
	public OpResponse entrustCharge(@RequestBody @Valid YYPayApiReq2<EntrustChargeReq> req) throws Exception {
		return chargeService.entrustCharge(req.getParams(), req.getMerchantCode());
	}

	/**
	 * 修改提现方式
	 * 
	 * @author buyi
	 * @date 2017-11-28 15:59:10
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@CheckSign
	@PostMapping("/api/fuiou/withdraw/modify_cash")
	public OpResponse modifyWithdrawType(@RequestBody @Valid YYPayApiReq2<ModifyWithdrawCashWayReq> req) {
		// 保存订单
		FuiouWithdrawModifyCashVO vo = fuiouWithdrawService.saveModifyWithdrawCashWay(req.getMerchantCode(), req.getParams());

		// 发送富友请求
		FuiouWithdrawModifyCash po = fuiouWithdrawService.doModifyWithdrawCashWay(vo);

		OpResponse opResponse = new OpResponse();
		opResponse.setCode(po.getRspCode());
		opResponse.setDisplay(po.getRspMsg());
		return opResponse;
	}

	/**
	 * 转账充值API
	 * 
	 * @author buyi
	 * @date 2018-06-04 15:48:29
	 * @since v2.2.1
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@CheckSign
	@PostMapping("/api/fuiou/charge/transfer")
	public OpResponse transferCharge(@RequestBody @Valid YYPayApiReq2<TransferChargeReq> req) throws Exception {

		// 持久化订单
		FuiouChargeVO vo = chargeInnerService.saveCharge(req.getMerchantCode(), req.getParams());
		if (!FuiouDepositStateEnum.INI.isValue(vo.getPo().getState())) {
			// 如果订单表不是初始化状态，则直接返回结果
			OpResponse opResponse = new OpResponse();
			opResponse.setCode(vo.getPo().getRspCode());
			opResponse.setDisplay(vo.getPo().getRspMsg());

			// 查询转账充值扩展信息
			TransferChargeRsp content = chargeInnerService.getTransferChargeRsp(vo.getPo());
			opResponse.setContent(content);
			return opResponse;
		}

		// 如果订单是初始，则请求富友无页面转账充值
		FuiouChargeTransferVO fuiouChargeTransferVO = chargeInnerService.doTransfer(vo);

		OpResponse opResponse = new OpResponse();
		opResponse.setCode(fuiouChargeTransferVO.getFuiouCharge().getRspCode());
		opResponse.setDisplay(fuiouChargeTransferVO.getFuiouCharge().getRspMsg());

		// 封装响应参数
		TransferChargeRsp content = chargeInnerService.getTransferChargeRsp(fuiouChargeTransferVO.getFuiouCharge(),
				fuiouChargeTransferVO.getFuiouChargeTransfer());
		opResponse.setContent(content);

		return opResponse;
	}
}
